/*
 *  NPCList.cpp
 *  SquirrelAdven
 *
 *  Created by Wen Xu on 3/7/11.
 *  Copyright 2011 __MyCompanyName__. All rights reserved.
 *
 */

#include "NPCList.h"
#include "fstream"
#include <string.h>


NPCList::NPCList(string filename) {
	this->filename = filename;
	parseData();
}

NPCList::~NPCList() {
}

void NPCList::parseData() {
	ifstream i_file;
	i_file.open(filename.c_str());
	
	string str;
	char *token;
	while (getline(i_file, str)) {
		// NPC
		if (str.find(":NPC") != -1) {
			// DEBUG
			std::cout << "find NPC data\n";
			// Read each NPC data block
			NPC * npc;
			
			while (getline(i_file, str) && str.find(":") == -1){
				if (str[str.length()-1] == '\r') // there is a \r in my line!
					str.erase(str.length()-1); // throw it away
				// BEGIN
				if (str.find("begin") != -1) {
					npc = new NPC;
					continue;
				}
				// ID
				if (str.find("ID") != -1) {
					int begin_index = str.find("ID")+strlen("ID");
					str = str.substr(begin_index, str.length()-begin_index);
					// convert to int
					npc->setCharacterID(atoi(str.c_str()));
					continue;
				}
				
				// TITLE
				if (str.find("Title") != -1) {
					int begin_index = str.find("Title")+strlen("Title")+1;
					str = str.substr(begin_index, str.length()-begin_index);
					npc->setCharacterTitle(str);
					continue;
				}
				
				
				// NAME
				if (str.find("Name") != -1) {
					int begin_index = str.find("Name")+strlen("Name")+1;
					str = str.substr(begin_index, str.length()-begin_index);
					npc->setCharacterName(str);
					continue;
				}
				
				
				// SPECIES
				if (str.find("Species") != -1) {
					int begin_index = str.find("Species")+strlen("Species")+1;
					str = str.substr(begin_index, str.length()-begin_index);
					npc->setCharacterSpecies(str);
					continue;
				}
				
				// BACKGROUND
				if (str.find("Background") != -1) {
					int begin_index = str.find("Background")+strlen("Background")+1;
					str = str.substr(begin_index, str.length()-begin_index);
					npc->setCharacterBackground(str);
					continue;
				}
				
				// OBJFILENAME
				if (str.find("ObjFilename") != -1) {
					int begin_index = str.find("ObjFilename")+strlen("ObjFilename")+1;
					str = str.substr(begin_index, str.length()-begin_index);
					npc->setObjFilename(str);
					continue;
				}
				
				// DIALOGFILENAME
				if (str.find("DialogFilename") != -1) {
					int begin_index = str.find("DialogFilename")+strlen("DialogFilename")+1;
					str = str.substr(begin_index, str.length()-begin_index);
					npc->setDialogFilename(str);
					continue;
				}
				
				// WMTFFILENAME
				if (str.find("Wmtf") != -1) {
					int begin_index = str.find("WmtfFilename")+strlen("WmtfFilename")+1;
					str = str.substr(begin_index, str.length()-begin_index);
					npc->setWmtfFilename(str);
					continue;
				}
				
				// TRANSLATE
				if (str.find("Translate") != -1) {
					int begin_index = str.find("Translate")+strlen("Translate")+1;
					str = str.substr(begin_index, str.length()-begin_index);
					
					// get translation value
					int i = 0;
					float t[3];
					token = strtok((char*)str.c_str(), " ");
					while (token != NULL) {
						t[i] = atof(token);
						token = strtok(NULL, " ");
						i++;
					}
					npc->setOriginalTranslate(t[0], t[1], t[2]);
					continue;
				}
				
				// SCALE
				if (str.find("Scale") != -1) {
					int begin_index = str.find("Scale")+strlen("Scale")+1;
					str = str.substr(begin_index, str.length()-begin_index);
					
					// get translation value
					int i = 0;
					float t[3];
					token = strtok((char*)str.c_str(), " ");
					while (token != NULL) {
						t[i] = atof(token);
						token = strtok(NULL, " ");
						i++;
					}
					npc->setOriginalScale(t[0], t[1], t[2]);
					continue;
				}
				
				
				// END
				if (str.find("end") != -1) {
					
					//npc->printData();
					
					npcList.push_back(*npc);
					continue;
				}
			};
			
		}
	}
	i_file.close();
}

vector<NPC> NPCList::getNPCList() {
	return npcList;
}

NPC * NPCList::getNPCByID(int ID) {
	int i;
	for (i = 0; i < npcList.size(); i++) {
		if (npcList[i].getCharacterID() == ID) {
			return &npcList[i];
		}
	}
	return NULL;
}

NPC * NPCList::getNPCByTitle(string title) {
	int i;
	for (i = 0; i < npcList.size(); i++) {
		if (npcList[i].getCharacterTitle() == title) {
			return &npcList[i];
		}
	}
	return NULL;
}

int NPCList::getNPCCount() {
	return npcList.size();
}